package com.example.secondhandspringboot.controller;


import com.example.secondhandspringboot.context.BaseContext;
import com.example.secondhandspringboot.domain.entity.ChatMessages;
import com.example.secondhandspringboot.domain.vo.ContactVO;
import com.example.secondhandspringboot.domain.vo.MessagesVO;
import com.example.secondhandspringboot.results.Result;
import com.example.secondhandspringboot.service.IChatMessagesService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周在林
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/chatMessages")
@Slf4j
@RequiredArgsConstructor
@CrossOrigin
public class ChatMessagesController {
    private final IChatMessagesService chatMessagesService;

    /**
     * 将信息设为已读
     * @param contactUserId
     * @return
     */
    @PatchMapping("/{contactUserId}")
    public Result setIsRead(@PathVariable Long contactUserId){
        Long currentUserId = BaseContext.getCurrentId();
        chatMessagesService.updateIdRead(currentUserId,contactUserId);
        return Result.success();
    }

    /**
     * 获取聊天记录
     * @param contactUserId
     * @return
     */
    @GetMapping("/{contactUserId}")
    public Result<List<MessagesVO>> getMessages(@PathVariable Long contactUserId){
        log.info("获取聊天记录");
        List<MessagesVO> messagesVOList = chatMessagesService.getMessages(contactUserId);
        return Result.success(messagesVOList);
    }

    /**
     * 获取所有未读通知数量
     * @return
     */
    @GetMapping("/getTotalNoticeNum")
    public Result<Integer> getTotalNoticeNum(){
        log.info("获取所有未读通知数量");
        int total = chatMessagesService.getTotalNoticeNum();
        return Result.success(total);
    }

}
